package com.nwpu.bigdata.service.impl;

import java.util.ArrayList;
import java.util.List;
import org.influxdb.dto.QueryResult;
import org.influxdb.dto.QueryResult.Result;
import org.influxdb.dto.QueryResult.Series;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.ImportResource;
import org.springframework.stereotype.Service;

import com.nwpu.bigdata.dto.Common;
import com.nwpu.bigdata.service.NetworkService;
import com.nwpu.bigdata.util.impl.InfluxDBUtilImpl;
import com.nwpu.bigdata.util.impl.InfluxdbService;



/**
 *@version:2018年6月6日上午11:34:13
*/
/**
 * @author Administrator
 *
 */
@Service
@ImportResource(locations= {"classpath:/xmlConfig/spring/applicationContext-util.xml"})
public class NetworkServiceImpl implements NetworkService{
	@Autowired
	private InfluxDBUtilImpl influDBClient;
	
	@Value("${retentionPolicy.cluster}")
	private String retentionPolicy;
	
	@Value("${inluxdb.k8s}")
	private String dbname;
	public List<Common> networkRxTx(){
			    	   	    
	    InfluxdbService service = new InfluxdbService(dbname,retentionPolicy, influDBClient.builder());
	    if(service.equals(null))
	    {
	    	return null;
	    }
	    //用于查询每一个节点的rx值
	    QueryResult queryResultRx = service.query("SELECT value FROM \"network/rx\" WHERE time > now() - 10m and type = 'node' group by nodename");//查询的某个指标
	    //用于查询每一个节点的tx值
	    QueryResult queryResultTx = service.query("SELECT value FROM \"network/tx\" WHERE time > now() - 10m and type = 'node' group by nodename");//查询的某个指标
	   
	    if(queryResultRx.getResults() == null && queryResultTx.getResults() == null) {
			System.out.println("the result is empty, end query!");
			return null; 
		}
	
	    //遍历结果得到相应的节点名和值,同时将结果添加到列表中
	    List<Common> lists = new ArrayList<Common>();
		for(Result resultRx : queryResultRx.getResults()){
		
	      List<Series> series = resultRx.getSeries();
	      if(series == null){
	    	  return null;
	      }
			for(Series serie : series) {
			 	  List<Double> listRx = new ArrayList<Double>();
				Common common = new Common();
				String nodename = serie.getTags().get("nodename");
			
				for(int i = 0; i < serie.getValues().size();i++){
				
					if(serie.getValues().get(i).get(1) == null){
						listRx.add(i,0.0);
					}
					else{
					listRx.add(i,(Double)serie.getValues().get(i).get(1)/(1024*1024*1024));
					}
				}
				common.setNodename(nodename+"rx");
				common.setValues(listRx);
				System.out.println(listRx);
				lists.add(common);
			}
			
		}
		//遍历结果得到相应的节点名和值，同时将结果添加到列表中
		for(Result resultTx : queryResultTx.getResults()){
		  
		      List<Series> series = resultTx.getSeries();
				if(series == null){
					return null;
				}
				for(Series serie : series) {
					List<Double> listTx = new ArrayList<Double>();
					Common common = new Common();
					String nodename = serie.getTags().get("nodename");
					System.out.println("节点名为："+nodename);
					for(int i = 0; i < serie.getValues().size();i++){
						if(serie.getValues().get(i).get(1) == null){
							listTx.add(i,0.0);
						}
						else{
						listTx.add(i,(Double)serie.getValues().get(i).get(1)/(1024*1024*1024));
						}
					}
					common.setNodename(nodename+"tx");
					common.setValues(listTx);
		
					lists.add(common);
					
				}
				
			}
		
		influDBClient.builder().close();
	    return lists;
	}
	
}
